using HSGD.Model.Validators;
using HSGD.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
//using WebApplication1;

namespace HSGD.Api.Controllers
{
    [ApiController]
    [Route("[controller]")]
    //[Authorize]
    public class ProductsController : ControllerBase
    {
        //private static readonly List<UserRegisterVo> Products = new();

        private readonly MinioFileService _fileService;

        public ProductsController(MinioFileService fileService)
        {
            var id = SnowFlakeSingle.Instance.NextId();
            _fileService = fileService;
        }

        //[HttpGet]
        //public ActionResult Get() {
        //    return Ok(Products);
        //}

        //[AllowAnonymous]
        //[HttpPost]
        //public IActionResult Create(UserRegisterVo product) {

        //    Products.Add(product);
        //    return Ok(Products);
        //}

        /// <summary>
        /// �ļ��ϴ�
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpPost]
        public async Task<IActionResult> Upload(IFormFile file)
        {
            try
            {
                var fileName = await _fileService.Upload(file);
                return Ok(new { FileName = fileName });
            }
            catch (Exception ex)
            {
                return BadRequest(ex.Message);
            }
        }

        [AllowAnonymous]
        [HttpGet]
        public async Task<FileResult> UViewAsync(string name)
        {
            var res = await _fileService.DownloadAsync(name);
            return File(res.Item1, res.Item2);
        }

        [AllowAnonymous]
        [HttpGet]
        [Route("/GetNextId")]
        public async Task<string> GetId()
        {
            await Task.CompletedTask;
            var id = SnowFlakeSingle.Instance.NextId();
            return id.ToString();
        }
    }
}
